package com.example.xd.tuiliu_test.videoplayer.player;

import android.view.Surface;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class IJKPlayer implements IVideoPlayer {

    IjkMediaPlayer mPlayer;
    private Surface surface;
    static IJKPlayer player;

    private IJKPlayer() {
        mPlayer = new IjkMediaPlayer();
    }

    public static IJKPlayer getInstance() {
        if (player == null) {
            synchronized (IJKPlayer.class) {
                if (player == null) {
                    player = new IJKPlayer();
                }
            }
        }
        return player;
    }


    @Override
    public void initPlayer() {

    }

    @Override
    public void setRender(Surface surface) {
        if (surface == null) {
            mPlayer.setSurface(null);
        } else {
            mPlayer.setSurface(surface);
        }
        this.surface = surface;
    }

    @Override
    public void releaseSurface() {
        surface = null;
    }

    @Override
    public void release() {
        mPlayer.release();
    }

    @Override
    public long getNetSpeed() {
        return mPlayer.getTcpSpeed();
    }

    @Override
    public void setSpeedPlaying(float speed) {
        mPlayer.setSpeed(speed);
        mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "soundtouch", 1);
    }


    @Override
    public void start() {
        mPlayer.start();
    }

    @Override
    public void stop() {
        mPlayer.stop();
    }

    @Override
    public void pause() {
        mPlayer.pause();
    }

    @Override
    public int getVideoWidth() {
        return mPlayer.getVideoWidth();
    }

    @Override
    public int getVideoHeight() {
        return mPlayer.getVideoHeight();
    }

    @Override
    public boolean isPlaying() {
        return mPlayer.isPlaying();
    }

    @Override
    public void seekTo(long time) {
        mPlayer.seekTo(time);
    }

    @Override
    public long getCurrentPosition() {
        return mPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return mPlayer.getDuration();
    }

    @Override
    public int getVideoSarNum() {
        return mPlayer.getVideoSarNum();
    }

    @Override
    public int getVideoSarDen() {
        return mPlayer.getVideoSarDen();
    }
}
